﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using Petaframe.Generation.Templates;

namespace Petaframe.Generation
{
    public class EnumGenerator : GeneratorBase
    {
        public EnumGenerator()
        {

        }

        #region GENERATE DATATYPE CLASSES

        public override void Generate()
        {
            CreateProjectDirectory();

            for (int i = 0; i < classList.Count; i++)
            {
                string path = String.Empty;
                string tableName = classList[i];
                tableName = tableName.Remove(0, (tableName.LastIndexOf('.') + 1));
                this.TableName = tableName;
                this.ClassName = tableName.Replace(" ", "");
                path = this.FilePath + "\\" + this.ClassName + ".cs";

                GenerateEnumCode(path);
            }

            GenerateProjectAndAssemblyFile();
        }

        private void CreateProjectDirectory()
        {
            if (!Directory.Exists(this.FilePath))
                Directory.CreateDirectory(this.FilePath);
        }

        private void GenerateProjectAndAssemblyFile()
        {
            GenerateEnumProjectFile();
            AssemblyInfoDosyasiniOlustur();
        }

        private void GenerateEnumProjectFile()
        {
            string projectFileContent = ProjectFileContent.Content;
            string references = String.Empty;
            string classNameList = String.Empty;

            for (int i = 0; i < classList.Count; i++)
            {
                string tableName = classList[i];
                tableName = tableName.Remove(0, (tableName.LastIndexOf('.') + 1));
                classNameList += String.Format("<Compile Include=\"{0}.cs\" />" + Environment.NewLine, tableName.Replace(" ", ""));
            }

            projectFileContent = String.Format(projectFileContent, Guid.NewGuid().ToString().ToUpper(), this.Namespace, this.Namespace, references, classNameList);
            File.WriteAllText(this.FilePath + "\\" + this.Namespace + ".csproj", projectFileContent);
        }

        private void AssemblyInfoDosyasiniOlustur()
        {
            string propertyFileContent = AssemblyInfoContent.Content;

            propertyFileContent = String.Format(propertyFileContent, this.Namespace, this.Namespace, Guid.NewGuid().ToString());
            Directory.CreateDirectory(this.FilePath + "\\Properties");
            File.WriteAllText(this.FilePath + "\\Properties\\AssemblyInfo.cs", propertyFileContent);
        }

        private void GenerateEnumCode(string path)
        {
            TabloBilgileriniAl();
            DataTable dtEnum = TabloEnumBilgisiniAl();

            string generationText = String.Empty;
            PetaframeEnumClassTextTemplate temp = new PetaframeEnumClassTextTemplate();
            temp.ClassName = this.ClassName;
            temp.Namespace = this.Namespace;
            temp.EnumName = this.EnumColumnName;
            temp.PrimaryKeyColumnName = String.IsNullOrEmpty(this.PrimaryKeyColumnName) ? String.Empty : this.PrimaryKeyColumnName;
            temp.DataInfo = dtEnum;
            generationText = temp.TransformText();

            File.WriteAllText(path, generationText);
        }

        private void TabloBilgileriniAl()
        {
            IDataProvider dataProvider = GetDataProvider();
            DataTable dt = dataProvider.TabloBilgileriniAl();
            this.PrimaryKeyColumnName = dataProvider.PrimaryKeyColumnName;
        }

        private DataTable TabloEnumBilgisiniAl()
        {
            IDataProvider dataProvider = GetDataProvider();
            DataTable dt = dataProvider.TabloEnumBilgisiniAl();
            this.EnumColumnName = dataProvider.EnumColumnName;
            return dt;
        }

        #endregion
    }
}
